home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / gdbm / testgdbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-14  |  12.8 KB  |  506 lines  |  [TEXT/MPS ]

  1. /* testgdbm.c - Driver program to test the database routines and to
  2.    help debug gdbm.  Uses inside information to show "system" information */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  6.  
  7.     GDBM is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     GDBM is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GDBM; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     You may contact the author by:
  22.        e-mail:  phil@cs.wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.         phone:  (206) 676-3035
  28.        
  29. *************************************************************************/
  30.  
  31.  
  32. #include "gdbmdefs.h"
  33. #include "gdbmerrno.h"
  34. #include "extern.h"
  35.  
  36. extern gdbm_error gdbm_errno;
  37.  
  38. extern char * gdbm_version;
  39.  
  40. /* For getopt. */
  41. extern char *optarg;
  42. extern int optind, opterr;
  43.  
  44. gdbm_file_info *gdbm_file;
  45.  
  46. /* Debug procedure to print the contents of the current hash bucket. */
  47. print_bucket (bucket, mesg)
  48.      hash_bucket *bucket;
  49.      char *mesg;
  50. {
  51.   int  index;
  52.  
  53.   printf ("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
  54.      mesg, bucket->bucket_bits, bucket->count);
  55.   printf ("     #    hash value     key size    data size     data adr  home\n");
  56.   for (index = 0; index < gdbm_file->header->bucket_elems; index++)
  57.     printf ("  %4d  %12x  %11d  %11d  %11d %5d\n", index,
  58.        bucket->h_table[index].hash_value,
  59.        bucket->h_table[index].key_size,
  60.        bucket->h_table[index].data_size,
  61.        bucket->h_table[index].data_pointer,
  62.        bucket->h_table[index].hash_value % gdbm_file->header->bucket_elems);
  63.  
  64.   printf ("\nAvail count = %1d\n", bucket->av_count);
  65.   printf ("Avail  adr     size\n");
  66.   for (index = 0; index < bucket->av_count; index++)
  67.     printf ("%9d%9d\n", bucket->bucket_avail[index].av_adr,
  68.                     bucket->bucket_avail[index].av_size);
  69. }
  70.  
  71.  
  72. _gdbm_print_avail_list (dbf)
  73.      gdbm_file_info *dbf;
  74. {
  75.   int temp;
  76.   int size;
  77.   avail_block *av_stk;
  78.  
  79.   /* Print the the header avail block.  */
  80.   printf ("\nheader block\nsize  = %d\ncount = %d\n",
  81.       dbf->header->avail.size, dbf->header->avail.count);
  82.   for (temp = 0; temp < dbf->header->avail.count; temp++)
  83.     {
  84.       printf ("  %15d   %10d \n", dbf->header->avail.av_table[temp].av_size,
  85.           dbf->header->avail.av_table[temp].av_adr);
  86.     }
  87.  
  88.   /* Initialize the variables for a pass throught the avail stack. */
  89.   temp = dbf->header->avail.next_block;
  90.   size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  91.       + sizeof (avail_block));
  92.   av_stk = (avail_block *) alloca (size);
  93.  
  94.   /* Print the stack. */
  95.   while (FALSE)
  96.     {
  97.       lseek (dbf->desc, temp, L_SET);
  98.       read  (dbf->desc, (char *) av_stk, size);
  99.  
  100.       /* Print the block! */
  101.       printf ("\nblock = %d\nsize  = %d\ncount = %d\n", temp,
  102.           av_stk->size, av_stk->count);
  103.       for (temp = 0; temp < av_stk->count; temp++)
  104.     {
  105.       printf ("  %15d   %10d \n", av_stk->av_table[temp].av_size,
  106.         av_stk->av_table[temp].av_adr);
  107.     }
  108.       temp = av_stk->next_block;
  109.     }
  110. }
  111.  
  112. _gdbm_print_bucket_cache (dbf)
  113.      gdbm_file_info *dbf;
  114. {
  115.   int index;
  116.   char changed;
  117.  
  118.   printf ("Bucket Cache:\n  Index:  Address  Changed  Data_Hash \n");
  119.   for (index=0; index < CACHE_SIZE; index++)
  120.     {
  121.       changed = dbf->bucket_cache[index].ca_changed;
  122.       printf ("  %5d:  %7d  %7s  %x\n",
  123.           index,
  124.           dbf->bucket_cache[index].ca_adr,
  125.           (changed ? "True" : "False"),
  126.           dbf->bucket_cache[index].ca_data.hash_val);
  127.     }
  128. }
  129.  
  130. void usage (s)
  131.      char *s;
  132. {
  133.   printf ("Usage: %s [-r or -n] [gdbm-file] \n",s);
  134.   exit (2);
  135. }
  136.  
  137.  
  138. /* The test program allows one to call all the routines plus the hash function.
  139.    The commands are single letter commands.  The user is prompted for all other
  140.    information.  See the help command (?) for a list of all commands. */
  141.  
  142. main (argc, argv)
  143.      int argc;
  144.      char *argv[];
  145.  
  146. {
  147.  
  148.   char  cmd_ch;
  149.  
  150.   datum key_data;
  151.   datum data_data;
  152.   datum return_data;
  153.  
  154.   char key_line[500];
  155.   char data_line[1000];
  156.  
  157.   char done = FALSE;
  158.   char opt;
  159.   char reader = FALSE;
  160.   char newdb = FALSE;
  161.  
  162.   char *file_name;
  163.  
  164.  
  165.   /* Argument checking. */
  166.   while ((opt = getopt (argc, argv, "rn")) != -1)
  167.     switch (opt) {
  168.     case 'r':  reader = TRUE;
  169.                if (newdb) usage (argv[0]);
  170.                break;
  171.     case 'n':  newdb = TRUE;
  172.                if (reader) usage (argv[0]);
  173.                break;
  174.     }
  175.   if (argc > optind+1) usage (argv[0]);
  176.  
  177.   if (optind < argc)
  178.     {
  179.       file_name = argv[optind];
  180.     }
  181.   else
  182.     {
  183.       file_name = "junk.gdbm";
  184.     }
  185.  
  186.   /* Initialize variables. */
  187.   key_data.dptr = NULL;
  188.   data_data.dptr = data_line;
  189.  
  190.   if (reader)
  191.     gdbm_file = gdbm_open (file_name, 512, GDBM_READER, 00664, NULL);
  192.   else if (newdb)
  193.     gdbm_file = gdbm_open (file_name, 512, GDBM_NEWDB, 00664, NULL);
  194.   else
  195.     gdbm_file = gdbm_open (file_name, 512, GDBM_WRCREAT, 00664, NULL);
  196.   if (gdbm_file == NULL)
  197.     {
  198.       if (gdbm_errno != GDBM_CANT_BE_WRITER)
  199.     printf ("gdbm_open failed.\n");
  200.       else
  201.     printf ("Can't open as a writer. \n");
  202.       exit (2);
  203.     }
  204.  
  205.   /* Welcome message. */
  206.   printf ("\nWelcome to the gdbm test program.  Type ? for help.\n\n");
  207.   
  208.   while (!done)
  209.     {
  210. #ifdef macintosh
  211.       printf ("com -> \n");
  212. #else
  213.       printf ("com -> ");
  214. #endif
  215.       cmd_ch = getchar ();
  216.       if (cmd_ch != '\n')
  217.     {
  218.       char temp;
  219.       do
  220.           temp = getchar ();
  221.       while (temp != '\n' && temp != EOF);
  222.     }
  223.       if (cmd_ch == EOF) cmd_ch = 'q';
  224.       switch (cmd_ch)
  225.     {
  226.     
  227.     /* Standard cases found in all test{dbm,ndbm,gdbm} programs. */
  228.     case '\n':
  229.       printf ("\n");
  230.       break;
  231.  
  232.     case 'c':
  233.       {
  234.         int temp;
  235.         temp = 0;
  236.         if (key_data.dptr != NULL) free (key_data.dptr);
  237.         return_data = gdbm_firstkey (gdbm_file);
  238.         while (return_data.dptr != NULL)
  239.           {
  240.         temp++;
  241.         key_data = return_data;
  242.         return_data = gdbm_nextkey (gdbm_file, key_data);
  243.         free (key_data.dptr);
  244.           }
  245.         printf ("There are %d items in the database.\n\n", temp);
  246.         key_data.dptr = NULL;
  247.       }
  248.       break;
  249.  
  250.     case 'd':
  251.       if (key_data.dptr != NULL) free (key_data.dptr);
  252. #ifdef macintosh
  253.       printf ("key ->\n");
  254. #else
  255.       printf ("key -> ");
  256. #endif
  257.       gets (key_line);
  258.       key_data.dptr = key_line;
  259.       key_data.dsize = strlen (key_line)+1;
  260.       if (gdbm_delete (gdbm_file, key_data) != 0)
  261.         printf ("Item not found or deleted\n");
  262.       printf ("\n");
  263.       key_data.dptr = NULL;
  264.       break;
  265.  
  266.     case 'f':
  267.       if (key_data.dptr != NULL) free (key_data.dptr);
  268. #ifdef macintosh
  269.       printf ("key ->\n");
  270. #else
  271.       printf ("key -> ");
  272. #endif
  273.       gets (key_line);
  274.       key_data.dptr = key_line;
  275.       key_data.dsize = strlen (key_line)+1;
  276.       return_data = gdbm_fetch (gdbm_file, key_data);
  277.       if (return_data.dptr != NULL)
  278.         {
  279.           printf ("data is ->%s\n\n", return_data.dptr);
  280.           free (return_data.dptr);
  281.         }
  282.       else
  283.         printf ("No such item found.\n\n");
  284.       key_data.dptr = NULL;
  285.       break;
  286.  
  287.     case 'n':
  288.       if (key_data.dptr != NULL) free (key_data.dptr);
  289. #ifdef macintosh
  290.       printf ("key ->\n");
  291. #else
  292.       printf ("key -> ");
  293. #endif
  294.       gets (key_line);
  295.       key_data.dptr = key_line;
  296.       key_data.dsize = strlen (key_line)+1;
  297.       return_data = gdbm_nextkey (gdbm_file, key_data);
  298.       if (return_data.dptr != NULL)
  299.         {
  300.           key_data = return_data;
  301.           printf ("key is  ->%s\n", key_data.dptr);
  302.           return_data = gdbm_fetch (gdbm_file, key_data);
  303.           printf ("data is ->%s\n\n", return_data.dptr);
  304.           free (return_data.dptr);
  305.         }
  306.       else
  307.         {
  308.           printf ("No such item found.\n\n");
  309.           key_data.dptr = NULL;
  310.         }
  311.       break;
  312.  
  313.     case 'q':
  314.       done = TRUE;
  315.       break;
  316.  
  317.     case 's':
  318.       if (key_data.dptr != NULL) free (key_data.dptr);
  319. #ifdef macintosh
  320.       printf ("key ->\n");
  321. #else
  322.       printf ("key -> ");
  323. #endif
  324.       gets (key_line);
  325.       key_data.dptr = key_line;
  326.       key_data.dsize = strlen (key_line)+1;
  327. #ifdef macintosh
  328.       printf ("data ->\n");
  329. #else
  330.       printf ("data -> ");
  331. #endif
  332.       gets (data_line);
  333.       data_data.dsize = strlen (data_line)+1;
  334.       if (gdbm_store (gdbm_file, key_data, data_data, GDBM_REPLACE) != 0)
  335.         printf ("Item not inserted. \n");
  336.       printf ("\n");
  337.       key_data.dptr = NULL;
  338.       break;
  339.  
  340.     case '1':
  341.       if (key_data.dptr != NULL) free (key_data.dptr);
  342.       key_data = gdbm_firstkey (gdbm_file);
  343.       if (key_data.dptr != NULL)
  344.         {
  345.           printf ("key is  ->%s\n", key_data.dptr);
  346.           return_data = gdbm_fetch (gdbm_file, key_data);
  347.           printf ("data is ->%s\n\n", return_data.dptr);
  348.           free (return_data.dptr);
  349.         }
  350.       else
  351.         printf ("No such item found.\n\n");
  352.       break;
  353.  
  354.     case '2':
  355.       return_data = gdbm_nextkey (gdbm_file, key_data);
  356.       if (return_data.dptr != NULL)
  357.         {
  358.           free (key_data.dptr);
  359.           key_data = return_data;
  360.           printf ("key is  ->%s\n", key_data.dptr);
  361.           return_data = gdbm_fetch (gdbm_file, key_data);
  362.           printf ("data is ->%s\n\n", return_data.dptr);
  363.           free (return_data.dptr);
  364.         }
  365.       else
  366.         printf ("No such item found.\n\n");
  367.       break;
  368.  
  369.  
  370.     /* Special cases for the testgdbm program. */
  371.     case 'r':
  372.       {
  373.         if (gdbm_reorganize (gdbm_file))
  374.           printf ("Reorganization failed. \n\n");
  375.         else
  376.           printf ("Reorganization succeeded. \n\n");
  377.       }
  378.       break;
  379.  
  380.     case 'A':
  381.       _gdbm_print_avail_list (gdbm_file);
  382.       printf ("\n");
  383.       break;
  384.  
  385.     case 'B':
  386.       {
  387.         int temp;
  388.         char number[80];
  389.  
  390. #ifdef macintosh
  391.         printf ("bucket?\n");
  392. #else
  393.         printf ("bucket? ");
  394. #endif
  395.         gets (number);
  396.         sscanf (number,"%d",&temp);
  397.  
  398.         if (temp >= gdbm_file->header->dir_size /4)
  399.           {
  400.         printf ("Not a bucket. \n\n");
  401.         break;
  402.           }
  403.         _gdbm_get_bucket (gdbm_file, temp);
  404.       }
  405.       printf ("Your bucket is now ");
  406.  
  407.     case 'C':
  408.       print_bucket (gdbm_file->bucket, "Current bucket");
  409.       printf ("\n current directory entry = %d.\n", gdbm_file->bucket_dir);
  410.       printf (" current bucket address  = %d.\n\n",
  411.           gdbm_file->cache_entry->ca_adr);
  412.       break;
  413.  
  414.     case 'D':
  415.       printf ("Hash table directory.\n");
  416.       printf ("  Size =  %d.  Bits = %d. \n\n",gdbm_file->header->dir_size,
  417.           gdbm_file->header->dir_bits);
  418.       {
  419.         int temp;
  420.  
  421.         for (temp = 0; temp < gdbm_file->header->dir_size / 4; temp++)
  422.           {
  423.         printf ("  %10d:  %12d\n", temp, gdbm_file->dir[temp]);
  424.         if ( (temp+1) % 20 == 0 && isatty (0))
  425.           {
  426.             printf ("*** CR to continue: ");
  427.             while (getchar () != '\n') /* Do nothing. */;
  428.           }
  429.           }
  430.       }
  431.       printf ("\n");
  432.       break;
  433.  
  434.     case 'F':
  435.       {
  436.         printf ("\nFile Header: \n\n");
  437.         printf ("  table        = %d\n", gdbm_file->header->dir);
  438.         printf ("  table size   = %d\n", gdbm_file->header->dir_size);
  439.         printf ("  table bits   = %d\n", gdbm_file->header->dir_bits);
  440.         printf ("  block size   = %d\n", gdbm_file->header->block_size);
  441.         printf ("  bucket elems = %d\n", gdbm_file->header->bucket_elems);
  442.         printf ("  bucket size  = %d\n", gdbm_file->header->bucket_size);
  443.         printf ("  header magic = %x\n", gdbm_file->header->header_magic);
  444.         printf ("  next block   = %d\n", gdbm_file->header->next_block);
  445.         printf ("  avail size   = %d\n", gdbm_file->header->avail.size);
  446.         printf ("  avail count  = %d\n", gdbm_file->header->avail.count);
  447.         printf ("  avail nx blk = %d\n", gdbm_file->header->avail.next_block);
  448.         printf ("\n");
  449.       }
  450.       break;
  451.  
  452.         case 'H':
  453.       if (key_data.dptr != NULL) free (key_data.dptr);
  454. #ifdef macintosh
  455.       printf ("key ->\n");
  456. #else
  457.       printf ("key -> ");
  458. #endif
  459.       gets (key_line);
  460.       key_data.dptr = key_line;
  461.       key_data.dsize = strlen (key_line)+1;
  462.       printf ("hash value = %x. \n\n", _gdbm_hash (key_data));
  463.       key_data.dptr = NULL;
  464.       break;
  465.  
  466.     case 'K':
  467.       _gdbm_print_bucket_cache (gdbm_file);
  468.       break;
  469.  
  470.     case 'V':
  471.       printf ("%s\n\n", gdbm_version);
  472.       break;
  473.  
  474.     case '?':
  475.       printf ("c - count (number of entries)\n");
  476.       printf ("d - delete\n");
  477.       printf ("f - fetch\n");
  478.       printf ("n - nextkey\n");
  479.       printf ("q - quit\n");
  480.       printf ("s - store\n");
  481.       printf ("1 - firstkey\n");
  482.       printf ("2 - nextkey on last key (from n, 1 or 2)\n\n");
  483.  
  484.       printf ("r - reorganize\n");
  485.       printf ("A - print avail list\n");
  486.       printf ("B - get and print current bucket n\n");
  487.       printf ("C - print current bucket\n");
  488.       printf ("D - print hash directory\n");
  489.       printf ("F - print file header\n");
  490.       printf ("H - hash value of key\n");
  491.       printf ("K - print the bucket cache\n");
  492.       printf ("V - print version of gdbm\n");
  493.       break;
  494.  
  495.     default:
  496.       printf ("What? \n\n");
  497.       break;
  498.  
  499.     }
  500.     }
  501.  
  502.   /* Quit normally. */
  503.   exit (0);
  504.  
  505. }
  506.